package com.zdw.oauth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zdw.common.parent.JSONResult;
import com.zdw.oauth.config.international.InternationalUtils;
import com.zdw.oauth.dto.PermissionAddDto;
import com.zdw.oauth.dto.PermissionSelectDto;
import com.zdw.oauth.dto.PermissionUpdDto;
import com.zdw.oauth.dto.RolePermissionDto;
import com.zdw.oauth.service.PermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 新增权限
     *
     * @return
     */
    @ApiOperation(value = "新增权限")
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckPermission(value = "permission.add", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult add(@RequestBody PermissionAddDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.add(dto));
    }

    /**
     * 编辑权限
     *
     * @return
     */
    @ApiOperation(value = "编辑权限")
    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = "permission.update", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult update(@RequestBody PermissionUpdDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.update(dto));
    }

    /**
     * 删除权限
     *
     * @return
     */
    @ApiOperation(value = "删除权限")
    @DeleteMapping("/delete")
    @SaCheckLogin
    @SaCheckPermission(value = "permission.delete", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult delete(@RequestBody List<Integer> permissionIdList) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.delete(permissionIdList));
    }

    /**
     * 查询权限信息
     *
     * @return
     */
    @ApiOperation(value = "查询权限信息")
    @PostMapping("/select")
    @SaCheckPermission(value = "permission.select", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult select(@RequestParam int id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.select(id));
    }

    /**
     * 分页查询权限列表
     *
     * @return
     */
    @ApiOperation(value = "分页查询权限列表")
    @PostMapping("/page")
    @SaCheckPermission(value = "permission.select", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult page(@RequestBody PermissionSelectDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.page(dto));
    }

    /**
     * 查询权限树
     *
     * @return
     */
    @ApiOperation(value = "查询权限树")
    @PostMapping("/tree")
    @SaCheckPermission(value = "permission.select", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult tree() {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.tree());
    }

    /**
     * 给角色分配权限
     *
     * @return
     */
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/grantPermission")
    @SaCheckPermission(value = "permission.grant", orRole = {"superAdmin", "admin", "permission"})
    public JSONResult grantPermission(@RequestBody RolePermissionDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), permissionService.grantPermission(dto));
    }

}
